//
//  YMSOAuthWellcomeController.swift
//  YMSWeibo
//
//  Created by 杨茂盛 on 2016/11/15.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//
//头像
//"http://tva3.sinaimg.cn/crop.0.17.1536.1536.50/005LWpPijw8f9oj401jkjj316o17nwix.jpg

//screen_name" = yms1230
import UIKit
import SnapKit
import SDWebImage
private let bottomMargin: CGFloat = 120

class YMSOAuthWellcomeController: UIViewController {
    var myModel:YMSUserinfoModel?
    
        override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        self.startAnnimation()
    }
    //创建动画
    func startAnnimation(){
        let offsetY = -(UIScreen.main.bounds.height - bottomMargin - iconImageView.bounds.height)
        //layoutIfNeeded() 提前刷新页面 viewDidLayoutSubviews
        self.iconImageView.snp.updateConstraints({ (make) in
            make.bottom.equalTo(self.view).offset(offsetY)
        })
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 7, options: [], animations: {
            //执行动画效果
            //更新约束
            //提前刷新页面
            self.view.layoutIfNeeded()
            
        }, completion: { (finished) -> () in
            //动画效果完成的回调
            UIView.animate(withDuration: 0.5, animations: {
                self.showLabel.alpha = 1
            }, completion: { (_) -> () in
                print("登录完成")
                //                UIApplication.shared.keyWindow?.rootViewController = YMSMainTabBarController()
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: choseRootViewController), object: nil)
            })
        })
    }
    //搭建界面
    private func setUI(){
        self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        self.view .addSubview(iconImageView)
        self.view .addSubview(showLabel)
        showLabel.setFontWithText(showLabel, text: "欢迎回来哦", color: UIColor.black, fontSize: 30)

        self.iconImageView.layer.cornerRadius = 65
        self.iconImageView.layer.masksToBounds = true
        //设置网络图片
        iconImageView.sd_setImage(with: YMSUserinfoViewModel.shared.headURL)

        
        //布局
        iconImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-bottomMargin)
            make.size.width.equalTo(130)
            make.size.height.equalTo(130)
        }
        
        showLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp.bottom).offset(20)
        }
        self.showLabel.alpha = 0
    }
    //懒加载控件
    private lazy var iconImageView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    private lazy var showLabel:UILabel = {
        let myLbl = UILabel()
        myLbl.setFontWithText(myLbl, text: "欢迎回来哦", color: UIColor.black, fontSize: 30)
        return myLbl
    }()
}
